package com.tensquare.article.controller;
/*
 *ClassName:ArticleController1
 *Description:基于MybatisPlus
 *@author:可爱的大鸭子
 */

import com.baomidou.mybatisplus.plugins.Page;
import com.tensquare.article.pojo.Article1;
import com.tensquare.article.service.ArticleService1;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/article1")
@CrossOrigin
public class ArticleController1 {
    @Autowired
    ArticleService1 articleService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private HttpServletRequest request;

    @RequestMapping(method = RequestMethod.GET)
    public Result findAll() {
        List list = articleService.findAll();
        return new Result(true, StatusCode.OK, "查询成功", list);
    }
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result findById(@PathVariable String id) {
        Article1 Article = articleService.findById(id);
        return new Result(true, StatusCode.OK, "查询成功", Article);
    }
    //新增标签数据接口
    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody Article1 article) {
        articleService.add(article);
        return new Result(true, StatusCode.OK, "添加成功");
    }
    //修改标签数据接口
    @RequestMapping(value = "{id}", method = RequestMethod.PUT)
    public Result update(@PathVariable String id, @RequestBody Article1 article) {
        article.setId(id);
        articleService.update(article);
        return new Result(true, StatusCode.OK, "修改成功");
    }
    //删除文章数据接口
    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable String id) {
        articleService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    @RequestMapping(value="/search1/{page}/{size}", method = RequestMethod.POST)
    public Result searcha(@RequestBody Map map, @PathVariable int page, @PathVariable int size) {
        Page page1 = articleService.search(map, page, size);
        return new Result(true, StatusCode.OK, "查询成功",
                new PageResult(page1.getTotal(), page1.getRecords()));
    }
    //条件查询：使用Mybatis Plus 提供的EntityWrapper对象封装where查询条件
    //查询新的推荐
    @RequestMapping(value="/search/{page}/{size}", method = RequestMethod.POST)
    public Result search(@RequestBody Map map, @PathVariable int page, @PathVariable int size) {
        Claims user_claims = (Claims)request.getAttribute("user_claims");
        if(user_claims==null){
            System.out.println("当前无用户登录");
            //当前无用户登录
            Page page1 = articleService.search(map, page, size);
            return new Result(true, StatusCode.OK, "查询成功",
                    new PageResult(page1.getTotal(), page1.getRecords()));
        }
        //当前有用户登录
        String uid=user_claims.getId();
        if(redisTemplate.boundValueOps("last_page_"+uid).get()==null){
            //当前用户是第一次登录
            redisTemplate.boundValueOps("last_page_"+uid).set(page);
            Page page1 = articleService.search(map, page, size);
            return new Result(true, StatusCode.OK, "查询成功",
                    new PageResult(page1.getTotal(), page1.getRecords()));
        }
        //当前用户有浏览历史
        int last_page=(int) redisTemplate.boundValueOps("last_page_"+uid).get();
        if(last_page==30){
            last_page=1;
        }
        Page page1 = articleService.search(map, last_page+1, size);
        redisTemplate.boundValueOps("last_page_"+uid).set(last_page+1); //更新
        return new Result(true, StatusCode.OK, "查询成功",
                new PageResult(last_page,page1.getTotal(), page1.getRecords()));
    }
    //查询旧的推荐
    @RequestMapping(value="/searchOld/{page}/{size}", method = RequestMethod.POST)
    public Result search1(@RequestBody Map map, @PathVariable int page, @PathVariable int size) {
        Claims user_claims = (Claims)request.getAttribute("user_claims");
        if(user_claims==null){
            System.out.println("当前无用户登录");
            //当前无用户登录
            Page page1 = articleService.search(map, page, size);
            return new Result(true, StatusCode.OK, "查询成功",
                    new PageResult(page1.getTotal(), page1.getRecords()));
        }
        //当前有用户登录
        String uid=user_claims.getId();
        if(redisTemplate.boundValueOps("last_page_"+uid).get()==null){
            //是第一次登录
            Page page1 = articleService.search(map, page, size);
            return new Result(true, StatusCode.OK, "查询成功",
                    new PageResult(page1.getTotal(), page1.getRecords()));
        }
        //当前用户有浏览历史
        int last_page=(int) redisTemplate.boundValueOps("last_page_"+uid).get();
        if(last_page==30){
            last_page=1;
        }
        Page page1 = articleService.search(map, last_page, size);
        return new Result(true, StatusCode.OK, "查询成功",
                new PageResult(page1.getTotal(), page1.getRecords()));
    }
    //刷新页面执行的查询
    @RequestMapping(value="/searchRefresh/{page}/{size}", method = RequestMethod.POST)
    public Result search2(@RequestBody Map map, @PathVariable int page, @PathVariable int size) {
        Claims user_claims = (Claims)request.getAttribute("user_claims");   //获取当前登录的用户id
        if(user_claims==null){
            //当前无用户登录
            Page page1 = articleService.search(map, page, size);
            return new Result(true, StatusCode.OK, "查询成功",
                    new PageResult(page1.getTotal(), page1.getRecords()));
        }
        //当前有用户登录并且已经有浏览历史了
        String uid=user_claims.getId();
        int last_page=(int) redisTemplate.boundValueOps("last_page_"+uid).get();
        if(last_page==30) last_page=1;
        Page page1 = articleService.search(map, last_page+1, size);
        redisTemplate.boundValueOps("last_page_"+uid).set(last_page+1); //更新
        return new Result(true, StatusCode.OK, "查询成功",
                new PageResult(last_page,page1.getTotal(), page1.getRecords()));
    }
}
